/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.approve.web;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.utils.excel.ExportExcel;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.approve.entity.HomeApprove;
import com.fosung.frame.modules.approve.entity.PartyStatistic;
import com.fosung.frame.modules.approve.service.HomeApproveService;
import com.fosung.frame.modules.approve.service.StatisticService;
import com.fosung.frame.modules.sys.utils.DictUtils;
import com.fosung.frame.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;

/**
 * 党组织统计Controller
 * @author ygl
 * @version 2017-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/approve/partyStatistic")
public class PartyStatisticController extends BaseController {

	@Autowired
	private StatisticService statisticService;
	
	@Autowired
	private HomeApproveService homeApproveService;
	
	@RequiresPermissions("approve:partyStatistic:view")
	@RequestMapping(value = {"list", ""})
	public String list(PartyStatistic partyStatistic, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(StringUtils.isEmpty(partyStatistic.getYear())) {
			if(DictUtils.findAssessmentTimeList().size()>0) {
				partyStatistic.setYear(DictUtils.findAssessmentTimeList().get(0).getValue());
			}else {
				Calendar calendar = Calendar.getInstance();
				partyStatistic.setYear(calendar.get(Calendar.YEAR)+"");
			}
		}
		partyStatistic.setUserOrgId(UserUtils.getUser().getOffice().getId());
		Page<PartyStatistic> page = statisticService.findPartyPmPage(new Page<PartyStatistic>(request, response), partyStatistic); 
		model.addAttribute("page", page);
		model.addAttribute("orgId", partyStatistic.getOrganizationId());
		model.addAttribute("typeId", partyStatistic.getTypeId());
		return "modules/approve/partyStatisticList";
	}

	@RequiresPermissions("approve:partyStatistic:view")
	@RequestMapping(value = "form")
	public String form(HomeApprove homeApprove, Model model,HttpServletRequest request, HttpServletResponse response) {
		Page<HomeApprove> page = homeApproveService.findPartyLifeByOrgId(new Page<HomeApprove>(request, response), homeApprove); 
		model.addAttribute("page", page);
		return "modules/approve/partyStatisticForm";
	}
	
	/**
	 * 查询党组织排名 前10名
	 * @param partyStatistic
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findPartyPmList")
	@ResponseBody
	public List<PartyStatistic> findPartyPmList(PartyStatistic partyStatistic, HttpServletRequest request, HttpServletResponse response, Model model) {
		String organizationId = UserUtils.getUser().getOffice().getId();
		partyStatistic.setOrganizationId(organizationId);
		List<PartyStatistic> list = statisticService.findPartyPmList(partyStatistic); 
		return list;
	}
	
	
	/**
	 * 根据社区类型查询下边的组织树
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findOrganizeNameByTypeId")
	@ResponseBody
	public List<Map<String,String>> findOrganizeNameByTypeId(PartyStatistic partyStatistic, HttpServletRequest request, HttpServletResponse response, Model model) {
		String organizationId = UserUtils.getUser().getOffice().getId();
		partyStatistic.setOrganizationId(organizationId);
		List<Map<String,String>> list = statisticService.findOrganizeNameByTypeId(partyStatistic); 
		return list;
	}
	
	/**
	 * 导出党组织积分统计Excel
	 * @param partyStatistic
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "exportExcel")
	public void exportExcel(PartyStatistic partyStatistic, HttpServletRequest request, HttpServletResponse response, Model model) {
		partyStatistic.setUserOrgId(UserUtils.getUser().getOffice().getId());
		Page<PartyStatistic> page = statisticService.findPartyPmPage(new Page<PartyStatistic>(request, response), partyStatistic); 
		List<PartyStatistic> listInfo = page.getList();
    	
		List<String> headerList = Lists.newArrayList();
		headerList.add("排名");
		headerList.add("所属支部");
		headerList.add("年份");
		headerList.add("积分");
		
		ExportExcel excel = new ExportExcel("党组织积分统计", headerList);
		for (int i = 0; i < listInfo.size(); i++) {
			Row row = excel.addRow();
			PartyStatistic obj = listInfo.get(i);
			excel.addCell(row, 0, obj.getNumPm(),2,Class.class);
			excel.addCell(row, 1, obj.getOrganizationName(),2,Class.class);
			excel.addCell(row, 2, obj.getYear(),2,Class.class);
			excel.addCell(row, 3, obj.getTotalScore(),2,Class.class);
		}
		try {
			excel.write(response,"党组织积分统计.xls");
		} catch (IOException e) {
			e.printStackTrace();
		}
		excel.dispose();
	}
	

}